package simri.desktop.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerAdapter;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


import simri.comunicacao.usb.ComunicacaoUSB;
import simri.desktop.auxiliar.TEnviarDados;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class JPAquisicao extends javax.swing.JPanel implements Runnable{
	private JLabel jLabelDadosAEnviar;
	private JButton jButtonEnviar;
	private JLabel jLabelDadosRecebidos;
	private JButton jbVoltar;
	private JButton jbCancel;
	private JScrollPane jScrollPane1;
	private JTextArea jTextAreaDados;
	private JButton jButtonConectar;
	private JTextField jTextFieldDadosAEnviar;
	private String PortaConexao;
	private int baudrate;
	private ComunicacaoUSB conectarSerial;
	private Thread thread;
	private Thread threadE;
	private int linhaAtual;
	private boolean conectado;
	
	private String ipConexao;
	
	private long tempoAq;
	private TEnviarDados ted;

	public void setPorta(String p){this.PortaConexao = p;}
	public void setBd(int bd){this.baudrate = bd;}

	public boolean getDados(){
		if(PortaConexao==null && baudrate==0){
			return false;
		}else{
			return true;

		}
	}

	public void ConectarSerial() {
		if(getDados()){
			conectarSerial = new ComunicacaoUSB(PortaConexao, baudrate, 0,tempoAq);
			conectarSerial.ObterIdDaPorta();
			conectarSerial.AbrirPorta();
			conectado = true;
			linhaAtual = 0;
			ted = new TEnviarDados(conectarSerial,conectado,tempoAq);
			thread = new Thread(this);
			threadE = new Thread(ted);
			conectarSerial.LerDados();
			thread.start();
			threadE.start();
			jButtonConectar.setText("Desconectar");
		}
		else{
			JOptionPane.showMessageDialog(null, "Nao foi possivel conectar, configure corretamente.");
		}
	}


	@SuppressWarnings("deprecation")
	public void DesconectarSerial() {
		if(getDados()){
			conectarSerial.FecharCom();
			if(thread != null)
			{
				conectado = false;
				thread.stop();
				threadE.stop();
				jButtonConectar.setText("Conectar");
			}
		}else{
			JOptionPane.showMessageDialog(null, "Nao foi possivel desconectar, configure corretamente.");
		}
	}

	/**
	 * Auto-generated main method to display this 
	 * JPanel inside a new JFrame.
	 */

	public JPAquisicao(JButton jbVoltar, String pc, int bdR, String ip, long temp) {
		super();
		PortaConexao = pc;
		baudrate = bdR;
		ipConexao = ip;
		tempoAq = temp;
		initGUI(jbVoltar);
	}

	private void initGUI(JButton jbVoltar) {
		try {
			setPreferredSize(new Dimension(800,600));
			setPreferredSize(getMaximumSize());
			this.setLayout(null);
			this.addContainerListener(new ContainerAdapter() {
			});
			{
				jLabelDadosAEnviar = new JLabel();
				this.add(jLabelDadosAEnviar);
				jLabelDadosAEnviar.setText("Dados a serem enviados:");
				jLabelDadosAEnviar.setBounds(10, 11, 187, 14);
			}
			{
				jTextFieldDadosAEnviar = new JTextField();
				this.add(jTextFieldDadosAEnviar);
				jTextFieldDadosAEnviar.setBounds(175, 8, 451, 20);
			}
			{
				jButtonEnviar = new JButton();
				this.add(jButtonEnviar);
				jButtonEnviar.setText("Enviar");
				jButtonEnviar.setBounds(636, 7, 128, 23);
				jButtonEnviar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						conectarSerial.HabilitarEscrita();
						conectarSerial.EnviarUmaString(jTextFieldDadosAEnviar.getText());
						conectarSerial.HabilitarLeitura();
						System.out.println(jTextFieldDadosAEnviar.getText());
						jTextFieldDadosAEnviar.setText("");
					}
				});
			}
			{
				jLabelDadosRecebidos = new JLabel();
				this.add(jLabelDadosRecebidos);
				jLabelDadosRecebidos.setText("Dados recebidos:");
				jLabelDadosRecebidos.setBounds(10, 36, 142, 14);
			}
			{
				jButtonConectar = new JButton();
				this.add(jButtonConectar);
				jButtonConectar.setText("Conectar");
				jButtonConectar.setBounds(584, 41, 179, 23);
				jButtonConectar.addActionListener(new ActionListener() {


					public void actionPerformed(ActionEvent evt) {
						if (!conectado){
							ConectarSerial();
						}
						else {
							DesconectarSerial();
						}
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				this.add(jScrollPane1);
				jScrollPane1.setBounds(19, 73, 744, 443);
				{
					jTextAreaDados = new JTextArea();
					jScrollPane1.setViewportView(jTextAreaDados);
					jTextAreaDados.setBounds(19, 73, 744, 476);
					jTextAreaDados.setEnabled(false);
					jTextAreaDados.setEditable(false);
				}
			}
			{
				//jbVoltar = new JButton("Voltar");
				this.add(jbVoltar);
				jbVoltar.setBounds(659, 542, 104, 23);
				jbVoltar.setName("Voltar");
			}
			
			//Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		while(conectado)
		{
			String leitura = conectarSerial.getDadosRecebidos();
			if (leitura != null){
				/*String [] dados = leitura.split("\n");
				for(int i=linhaAtual; i< dados.length; i++)
				{
					if(dados[i].trim().length() != 0)
					{
						System.out.println(dados[i].trim());
					}
				}
				linhaAtual = dados.length;*/
				jTextAreaDados.setText(leitura);
			}
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
